package cn.tedu._05mvcboot031.controller;

import cn.tedu._05mvcboot031.common.response.JsonResult;
import cn.tedu._05mvcboot031.common.response.StatusCode;
import cn.tedu._05mvcboot031.mapper.UserMapper;
import cn.tedu._05mvcboot031.pojo.dto.UserArticleDTO;
import cn.tedu._05mvcboot031.pojo.dto.UserLoginDTO;
import cn.tedu._05mvcboot031.pojo.entity.Article;
import cn.tedu._05mvcboot031.pojo.vo.ArticleVO;
import cn.tedu._05mvcboot031.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Validated
@Slf4j
@Api(tags = "01.用户模块")
@RestController
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "登录功能")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session) {
        /*
            1.打印日志消息;
            2.校验用户名和密码
            3.返回响应
         */
        log.debug("userLoginDTO = " + userLoginDTO);
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();

        UserVO userVO = userMapper.selectByUser(username, password);
        log.debug("userVO = " + userVO);
        if (userVO == null) {//用户名或密码错误
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }

        //会话
        session.setAttribute("user", userVO);

        return JsonResult.ok();
    }

    @ApiOperation(value = "发表文章功能")
    @PostMapping("article")
    public JsonResult article(@RequestBody @Validated UserArticleDTO userArticleDTO, @ApiIgnore HttpSession session) {
        log.debug("userArticleDTO = " + userArticleDTO);
        //登录状态校验
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {//未登录
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }

        //文章表中插入1条数据
        Article article = new Article();
        BeanUtils.copyProperties(userArticleDTO, article);
        article.setUserId(userVO.getId());
        userMapper.insertArticle(article);

        return JsonResult.ok();
    }

    @ApiOperation(value = "查询指定文章功能")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "int")
    @GetMapping("selectByArticleId")
    public JsonResult selectByArticleId(@Range(min = 1, max = 100, message = "文章ID必须在1-100之间") @RequestParam Integer id) {
        log.debug("id = " + id);
        ArticleVO articleVO = userMapper.selectByArticleId(id);

        return JsonResult.ok(articleVO);
    }
}









